/*
 * <p>$Id: //depot/MarketData/netlink/main/src/main/java/com/ebs/md/netlink/management/UpstreamConnectionListener.java#7 $ </p>
 * <p>$Change: 67381 $ </p>
 * <p>$Author: ygrinb $ </p>
 * <p>$Date: 2008/11/10 $ </p>
 *
 * Copyright 2007 EBS Dealing Resources Inc.
 * All Rights Reserved
 */

package com.ebs.md.fxmonitor.management;

import javax.jms.JMSException;
import javax.management.Notification;

import org.apache.log4j.Logger;

import com.ebs.commons.comp.IParsedPayload;
import com.ebs.commons.util.SysDump;
//import com.ebs.parser.api.IParsedPayload;
import com.ebs.md.fxmonitor.client.api.NetlinkFixConstants;
import com.ebs.md.fxmonitor.topics.ClientTopicKeys;
import com.ebs.md.fxmonitor.topics.ServerTopicKeys;

public class UpstreamConnectionListener extends AbstractConnectionListener
                                        implements UpstreamConnectionListenerMBean {
    private static final Logger s_logger = Logger.getLogger(SysDump.getCallerClass());
    
    final short EVENT_DOWN = 256;
    final short EVENT_CONNECTED = 257;
    final short EVENT_SIGNED_ON = 258;
    final short EVENT_RECOVERY_REQUESTED = 259;
    final short EVENT_RECOVERING = 260;
    final short EVENT_RUNNING = 261;
    final short EVENT_ARB_UP_LIVE = 1;
    final short EVENT_ARB_DOWN = 2;
    final short EVENT_ARB_UP_MOCK = 13;

    @Override
    protected Notification createNotification(IParsedPayload payload) throws JMSException {

        Notification    notification    = null;
        String          reason          = payload.getParsedStringField(ClientTopicKeys.ConnectionStatusText);

        notification = new Notification("Connection.Upstream.Error", getObjectName(), getNextSeqNo(), System.currentTimeMillis(), reason);

        s_logger.debug("ConnectionListener.createNotification() returning: " + notification);
        return notification;
    }

    @Override
    protected boolean processPayload(IParsedPayload payload) {
        boolean process     = false;
        String  messageType = payload.getParsedStringField(ServerTopicKeys.FIX_NAME_TAG);
        if(messageType.equalsIgnoreCase(NetlinkFixConstants.SubtypeConnectionStatusResponse)) { //events
            int event_code  = payload.getParsedShortField(ClientTopicKeys.EventCode);

            switch(event_code){
                case EVENT_ARB_DOWN:
                    payload.addField(ClientTopicKeys.ConnectionStatusText, "Arb Down");
                    process     = true;
                    break;
                case EVENT_DOWN:
                    payload.addField(ClientTopicKeys.ConnectionStatusText, "Upstream Connection Down");
                    process     = true;
                    break;
                default:
                    s_logger.debug("UpstreamConnectionListener.processPayload() not processing event code: " + event_code);
            }
        }

        s_logger.debug("UpstreamConnectionListener.processPayload() return " + process);
        return process;
    }
}
